package com.sgu6.sgu6_machine.ui.activity;

import android.app.AlertDialog;
import android.graphics.Color;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.DialogFragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.sgu6.serialport.api.SerialPortFinder;
import com.sgu6.serialport.dto.DeviceDto;
import com.sgu6.sgu6_machine.R;
import com.sgu6.sgu6_machine.adapter.ComAdapter;
import com.sgu6.sgu6_machine.base.BaseA;
import com.sgu6.sgu6_machine.config.MachineConfig;
import com.sgu6.sgu6_machine.container.MachineContainer;
import com.sgu6.sgu6_machine.event.CardComEvent;
import com.sgu6.sgu6_machine.event.ComEvent;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ComSelectA extends BaseA  {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.tvComName)
    TextView tvComName;
    @BindView(R.id.rvComDevice)
    RecyclerView rvComDevice;
    @BindView(R.id.tvCardComName)
    TextView tvCardComName;
    @BindView(R.id.rvCardComDevice)
    RecyclerView rvCardComDevice;

    private ComAdapter comAdapter, cardComAdapter;
    private List<DeviceDto> devices;
    private AlertDialog.Builder dialog1, dialog2;
    private int curComPosition = 0, curCardComPosition = 0;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_com_select);
        ButterKnife.bind(this);

        initUI();
    }


    @Override
    public void initUI() {

        initToolbar();

        initDefaultCom();

        initDialog();

        SerialPortFinder serialPortFinder = new SerialPortFinder();

        devices = serialPortFinder.getDevices();

        comAdapter = new ComAdapter(this, devices);
        cardComAdapter = new ComAdapter(this, devices);

        comAdapter.setOnClickListener(new ComAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
                curComPosition = position;
                dialog1.setMessage("是否选择串口[" + devices.get(curComPosition).getName() + "]?");
                dialog1.show();
            }
        });
        cardComAdapter.setOnClickListener(new ComAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
                curCardComPosition = position;
                dialog2.setMessage("是否选择串口[" + devices.get(curCardComPosition).getName() + "]?");
                dialog2.show();
            }
        });

        rvComDevice.setAdapter(comAdapter);
        rvCardComDevice.setAdapter(cardComAdapter);

        rvComDevice.setLayoutManager(new LinearLayoutManager(this));
        rvCardComDevice.setLayoutManager(new LinearLayoutManager(this));
    }

    private void initDialog() {
        dialog1 = new AlertDialog.Builder(this);
        dialog1.setTitle("提示");
        dialog1.setNegativeButton("取消", (dialog, which) -> {
        });

        dialog1.setPositiveButton("确定", (dialog, which) -> {
            EventBus.getDefault().post(new ComEvent(devices.get(curComPosition)));
            tvComName.setText(devices.get(curComPosition).getName());
        });

        dialog2 = new AlertDialog.Builder(this);
        dialog2.setTitle("提示");
        dialog2.setNegativeButton("取消", (dialog, which) -> {
        });

        dialog2.setPositiveButton("确定", (dialog, which) -> {
            EventBus.getDefault().post(new CardComEvent(devices.get(curCardComPosition)));
            tvCardComName.setText(devices.get(curCardComPosition).getName());
        });
    }

    private void initDefaultCom() {
        int len1 = MachineContainer.machineConfigDto.comPath.length();
        tvComName.setText("串口".concat(MachineContainer.machineConfigDto.comPath.substring(len1 - 1)));
        int len2 = MachineContainer.machineConfigDto.cardComPath.length();
        tvCardComName.setText("串口".concat(MachineContainer.machineConfigDto.cardComPath.substring(len2 - 1)));
    }

    private void initToolbar() {
        toolbar.setTitle("串口设置");
        toolbar.setTitleTextColor(Color.WHITE);
        toolbar.setBackgroundResource(R.color.colorPrimaryDark);
        setSupportActionBar(toolbar);

        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowTitleEnabled(true);
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        return true;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
